package logic

import (
	"context"
	"errors"
	"mall_srv/models"

	"mall_srv/good"
	"mall_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 用户登录
func (l *LoginLogic) Login(in *good.LoginRequest) (*good.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	user.Account = in.Account
	if err := user.GetUserInfoByAccount(); err != nil {
		logx.Info("该用户不存在，登录失败")
		return nil, err
	}
	if user.Password != in.Password {
		logx.Info("密码错误")
		return nil, errors.New("密码错误")
	}
	return &good.LoginResponse{
		Id: int64(user.ID),
	}, nil
}
